<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-07-04 15:58:45
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\System;

use App\Http\Services\Service;
use App\Models\RoleTable;
use App\Repositories\RoleRepository;
use App\Repositories\TableRepository;
use Illuminate\Http\Request;

/**
 * Summary of RoleService
 * @property RoleRepository $repository
 * @property TableRepository $table
 * @author sunkaiyuan
 */
class RoleService extends Service
    {
    public function boot(RoleRepository $repository, TableRepository $table)
        {
        $this->repository = $repository;
        $this->table      = $table;
        }
    public function list()
        {
        return $this->repository->getGrid();
        }
    protected function save($roledata)
        {
        return $this->repository->autosave($roledata);
        }

    protected function delete()
        {
        return $this->repository->delete($this->getRequestIds());
        }
    public function menu($request)
        {
        return $this->repository->menu($this->roleArray($request));
        }
    private function roleArray($request = null)
        {
        $request = $request ?? request();
        $ids     = [];
        if ($request->has("role_id"))
            $ids = [$request->role_id];
        else
            $ids = $request->role_ids;
        if ($request->has("filter")) {
            $filters = $request->filter;
            foreach ($filters ?? [] as $keys => $filter) {
                foreach ($filter ?? [] as $key => $value)
                    if ($key == 'role_id') {
                        $ids = [$value];
                        unset($filters[$keys]['role_id']);
                        break;
                        }
                }
            $request->offsetSet("filter", $filters);
            }
        return $ids;
        }
    public function table_right_btn()
        {
        return $this->repository->whereIn("id", $this->roleArray())->first()->role_table()->with("table")->get();
        }
    public function tablelist()
        {
        $table_ids = $this->repository->table_right_btn($this->roleArray())->pluck("id");

        return $this->table->condition()->whereNotIn("id", $table_ids)->getGrid();
        }
    public function department($request)
        {
        return $this->repository->department($this->roleArray($request));
        }
    public function column()
        {
        return $this->repository->column($this->roleArray(request()));
        }
    public function api()
        {
        return $this->repository->api($this->roleArray(request()));
        }
    protected function enableApi(Request $request)
        {
        return $this->repository->enableApi($request->toArray());
        }
    protected function disableApi(Request $request)
        {
        return $this->repository->disableApi($request->toArray());
        }
    protected function savePermission(Request $request)
        {
        return $this->repository->savePermission($request->toArray());
        }

    protected function saveroletable()
        {
        $request               = request();
        $data                  = RoleTable::whereId($request->id)->first();
        $data->setting_auth    = $request->setting_auth;
        $data->export_auth     = $request->export_auth;
        $data->showFooter_auth = $request->showFooter_auth;
        $data->save();
        return $data;
        }
    protected function deletetable()
        {
        return RoleTable::whereIn("id", $this->getRequestIds())->get()->filter(fn($v) => $v->delete());
        }
    protected function savetable()
        {
        $data = request()->toArray();
        return $this->repository->saveManyToMany($data['table_ids'] ?? [], $data['role_id'], RoleTable::class, "table_id", 'role_id', false);
        }
    }